package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.ToolsUtils;
import zhihuilan.entity.MyAcquisition;
import zhihuilan.mapper.MyAcquisitionMapper;
import zhihuilan.params.query.MyAcquisitionQueryParam;
import zhihuilan.params.save.MyAcquisitionSaveParam;
import zhihuilan.service.MyAcquisitionService;

import java.util.List;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyAcquisitionServiceImpl extends ServiceImpl<MyAcquisitionMapper, MyAcquisition> implements MyAcquisitionService {


    @Override
    public Page<MyAcquisition> selectPageList(MyAcquisitionQueryParam queryParam) {
        QueryWrapper<MyAcquisition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staff_id", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getKeyword())) {
                String keywords = queryParam.getKeyword();
                String keyword = keywords.replace("*", "_").replace("x", "_").replace("X", "_")
                        .replace("×", "_").replace("✘", "_").replace("＊", "_")
                        .replace(" ", "_").replace("/", "_").replace("-", "_");
                queryWrapper.and(wp -> wp.like("title", keyword).or().like("hyaddress", keyword).or().like(" LOWER(CONVERT(content USING utf8)) ", keyword.toLowerCase()));
//                List<String> keywordList = ToolsUtils.jiebaUtils(keywords);
//                String sqlWhere ="";
//                for(String item:keywordList){
//                    if(!sqlWhere.equals("")){
//                        sqlWhere= sqlWhere + " or ";
//                    }
//                    sqlWhere=sqlWhere + "title like'"+item+"' or LOWER(CONVERT(content USING utf8)) like'%"+item+"%' or hyaddress like'"+item+"'";
//                }
//                String finalSqlWhere = sqlWhere;
//                queryWrapper.and(wp-> wp.apply(finalSqlWhere));
            }
        }
        queryWrapper.orderByDesc("create_time");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyAcquisition> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Page<MyAcquisition> selectMyAcquisition(MyAcquisitionQueryParam queryParam) {
        QueryWrapper<MyAcquisition> queryWrapper = new QueryWrapper<>();
        String keyword = "";
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getKeyword())) {
                String keywords = queryParam.getKeyword();
                keyword = keywords.replace("*", "_").replace("x", "_").replace("X", "_")
                        .replace("×", "_").replace("✘", "_").replace("＊", "_")
                        .replace(" ", "_").replace("/", "_").replace("-", "_");
            }
        }
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        if (pageNum > 0) {
            pageNum = pageSize * (pageNum - 1);
        }
        Page<MyAcquisition> page = new Page<>(pageNum, pageSize);
        List<MyAcquisition> selectList = this.baseMapper.selectMyAcquisitionPage(pageNum, pageSize, keyword, queryParam.getStaffId());
        Integer count = this.baseMapper.selectMyAcquisitionCount(keyword, queryParam.getStaffId());
        page.setRecords(selectList);
        page.setTotal(count);
        return page;
    }

    @Override
    public MyAcquisition selectById(Integer id) {
        MyAcquisition myAcquisition = this.baseMapper.selectById(id);
        return myAcquisition;
    }

    @Override
    public MyAcquisition updateEntity(MyAcquisition entity) {
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Transactional
    @Override
    public MyAcquisition addEntity(MyAcquisitionSaveParam entity) {
        MyAcquisition myAcquisition = new MyAcquisition();
        try {
            BeanUtils.copyProperties(entity, myAcquisition);
            this.baseMapper.insert(myAcquisition);
            myAcquisition.setTitle("zhl-" + myAcquisition.getId());
            this.baseMapper.updateById(myAcquisition);
            return myAcquisition;
        } catch (RuntimeException e) {
            throw new RuntimeException("操作失败：" + e.getMessage());
        }
    }

    @Override
    public Long getTodaySendCount(MyAcquisitionQueryParam queryParam) {
        QueryWrapper<MyAcquisition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id", queryParam.getStaffId());
        queryWrapper.gt("create_time", queryParam.getCreateTime());
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Transactional
    @Override
    public Boolean deleteById(Integer id) {
        try {
            MyAcquisition myAcquisition = this.selectById(id);
            myAcquisition.setStatus(0);
            int count = this.baseMapper.updateById(myAcquisition);
            Integer deleteMsg = this.baseMapper.deleteMyAcquisitionMsg(id);
            if (count > 0 && deleteMsg >= 0) {
                return true;
            } else {
                return false;
            }
        } catch (RuntimeException e) {
            throw new RuntimeException("操作失败：" + e.getMessage());
        }
    }
}
